$(function () {
    InitTable();
    //根据窗口调整表格高度
    $(window).resize(function() {

        $('#carLogTab').bootstrapTable('resetView', {
            height: tableHeight()
        })
    });
    $("#searchCarLogBtn").click(function () {
        InitTable();
    });
    function InitTable() {
        $('#carLogTab').bootstrapTable('destroy');
        $('#carLogTab').bootstrapTable({
            url: "/carlog/selectAllUseCar",//数据源
            height: tableHeight(),//高度调整
            //search: true,//是否搜索
            pagination: true,//是否分页
            pageSize: 8,//单页记录数
            pageList: [5, 10, 20, 50],//分页步进值
            sidePagination: "client",//服务端分页
            contentType: "application/x-www-form-urlencoded",//请求数据内容格式 默认是 application/json 自己根据格式自行服务端处理
            dataType: "json",//期待返回数据类型
            method: "post",//请求方式
            //searchAlign: "right",//查询框对齐方式
            queryParamsType: "limit",//查询参数组织方式
            uniqueId: "cl_id",
            queryParams: function getParams(params) {
                return {
                    cl_num:$("#cl_numSearch").val(),
                    cl_name:$("#cl_nameSearch").val(),
                    cl_outtime:$("#cl_outtimeSearch").val(),
                    cl_company:$("#cl_companySearch").val(),
                    cl_days:$("#cl_daysSearch").val()
                }
            },
            searchOnEnterKey: false,//回车搜索
            //showRefresh: true,//刷新按钮
            //showColumns: true,//列选择按钮
            buttonsAlign: "right",//按钮对齐方式
            toolbar: "toolbar", //指定工具栏
            toolbarAlign: "right",//工具栏对齐方式
            columns: [
                {
                    title: "全选",
                    field: "select",
                    checkbox: true,
                    width: 20,//宽度
                    align: "center",//水平
                    valign: "middle"//垂直
                },
                {
                    field: "cl_id",//键名
                    title: "id",//标题
                    sortable: true,//是否可排序
                    order: "desc"//默认排序方式
                },
                {
                    field: "cl_num",
                    title: "车牌号",
                    algin:"center",
                    sortable: true
                },
                {
                    field: "cl_name",
                    title: "驾驶员",
                    sortable: true
                },
                {
                    field: "cl_outtime",
                    title: "使用时间",
                    width:25,
                    align:"center",
                    sortable: true
                },

                {
                    field: "cl_company",
                    title: "使用单位",
                    sortable: true
                },
                {
                    field: "cl_days",
                    title: "使用天数",
                    sortable: true
                },
                {
                    field: "cl_price",
                    title: "使用价格",
                    width:30,
                    align:"center",
                    sortable: true
                },
                {
                    field: "clstate_name",
                    title: "状态",
                    sortable: true,
                },
                {
                    field: "cl_oilmoney",
                    title: "油价",
                    sortable: true
                }

            ],
            onClickRow: function (row, $element) {
                //$element是当前tr的jquery对象
                $element.css("background-color", "#cecece");
            },//单击row事件
            locale: "zh-CN", //中文支持
            detailView: false, //是否显示详情折叠
            /*detailFormatter: function(index, row, element) {
                var html = '';
                $.each(row, function(key, val){
                    html += "<p>" + key + ":" + val +  "</p>"
                });
                return html;
            }*/
        });
    }
    function tableHeight() {
        return $(window).height() - 50;
    }
    $("#cl_outtimeSearch").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });

    $("#cl_outtime").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });
    $("#addCarLogBtn").click(function () {
        var cl_id = $("#cl_id").val();
        if (cl_id ===""){
            $("#myDialogHeaderLable").text("添加记录");
            $("#addRecord").text("添加");
        }
    });
    $("#updateCarLogBtn").click(function () {
        $("#addModal").modal("show");
        $("#myDialogHeaderLable").text("更新记录");
        $("#addRecord").text("更新");
        var table = $("#carLogTab").bootstrapTable("getSelections");
        if(table !=undefined && table.length === 1){
            var result = table[0];
            $("#cl_id").val(result.cl_id),
            $("#cl_num").val(result.cl_num);
            $("#cl_name").val(result.cl_name),
            $("#cl_outtime").val(result.cl_outtime),
            $("#cl_company").val(result.cl_company),
            $("#cl_days").val(result.cl_days),
            $("#cl_price").val(result.cl_price),
            $("#cl_state").val(result.cl_state),
            $("#cl_oilmoney").val(result.cl_oilmoney)
        }
    });
    $("#addRecord").click(function () {
        var cl_id = $("#cl_id").val();
        if (cl_id===""){
            $.ajax({
                url:"/carlog/insertCarLogs",
                type:"post",
                dataType:"json",
                data:{
                    cl_num:$("#cl_num").val(),
                    cl_name:$("#cl_name").val(),
                    cl_outtime:$("#cl_outtime").val(),
                    cl_company:$("#cl_company").val(),
                    cl_days:$("#cl_days").val(),
                    cl_price:$("#cl_price").val(),
                    cl_state:$("#cl_state").val(),
                    cl_oilmoney:$("#cl_oilmoney").val()
                },
                success:function (result) {
                    var error = result.errorCode;
                    if (error == 0){
                        toastr.success(result.status);
                        $("#addModal").modal("hide");
                        $("#carLogTab").bootstrapTable("refresh");
                        clearModelData();
                    }else {
                        toastr.error("插入失败")
                    }
                }
            })
        }else {
            $.ajax({
                url:"/carlog/updateCarlog",
                type:"post",
                dataType:"json",
                data: {
                    cl_id:$("#cl_id").val(),
                    cl_num: $("#cl_num").val(),
                    cl_name: $("#cl_name").val(),
                    cl_outtime: $("#cl_outtime").val(),
                    cl_company: $("#cl_company").val(),
                    cl_days: $("#cl_days").val(),
                    cl_price: $("#cl_price").val(),
                    cl_state: $("#cl_state").val(),
                    cl_oilmoney: $("#cl_oilmoney").val()
                },
                success:function (result) {
                    if (result.errorCode == 0){
                        toastr.success(result.status);
                        $("#addModal").modal("hide");
                        $("#carLogTab").bootstrapTable("refresh");
                        clearModelData();
                    }else {
                        toastr.error("更新失败")
                    }
                }
            })

        }
    });
    function clearModelData() {
        $("#cl_num").val("");
        $("#cl_name").val(""),
        $("#cl_outtime").val(""),
        $("#cl_company").val(""),
        $("#cl_days").val(""),
        $("#cl_price").val(""),
        $("#cl_state").val(""),
        $("#cl_id").val("")
    }
});
